import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

import Layout from '@/layout'

export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/auth-redirect',
    component: () => import('@/views/login/auth-redirect'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: 'Dashboard',
        meta: { title: '首页', icon: 'dashboard', affix: true }
      }
    ]
  },
  {
    path: '/querydata',
    component: Layout,
    children: [
      {
        path: 'index',
        component: () => import('@/views/querydata/index'),
        name: 'QueryData',
        meta: { title: '生产数据', icon: 'documentation', affix: false }
      }
    ]
  },
  {
    path: '/datacharts',
    component: Layout,
    children: [
      {
        path: 'index',
        component: () => import('@/views/datacharts/index'),
        name: 'DataCharts',
        meta: { title: '生产曲线', icon: 'chart', affix: false }
      }
    ]
  },
  {
    path: '/editdata',
    component: Layout,
    children: [
      {
        path: 'index',
        component: () => import('@/views/editdata/index'),
        name: 'EditData',
        meta: { title: '数据编辑', icon: 'edit', affix: false }
      }
    ]
  },
  {
    path: '/farmmanage',
    component: Layout,
    redirect: '/farmmanage/batch',
    meta: {
      title: '农场编辑',
      icon: 'form',
      roles: ['admin', 'editor'] // you can set roles in root nav
    },
    children: [
      {
        path: 'batch',
        component: () => import('@/views/farmmanage/batchmanage.vue'),
        name: 'BatchManage',
        meta: {
          title: '批次管理',
          icon: 'list',
          affix: false,
          roles: ['admin'] // or you can only set roles in sub nav
        }
      },
      {
        path: 'user',
        component: () => import('@/views/farmmanage/usermanage.vue'),
        name: 'UserManage',
        meta: {
          title: '用户管理',
          affix: false,
          icon: 'user'
        }
      }]
  },
  {
    path: '/profile',
    component: Layout,
    redirect: '/profile/index',
    hidden: true,
    children: [
      {
        path: 'index',
        component: () => import('@/views/profile/index'),
        name: 'Profile',
        meta: { title: '个人信息', icon: 'user', noCache: true }
      }
    ]
  }
]

export const asyncRoutes = [
  // {
  //   path: '/permission',
  //   component: Layout,
  //   redirect: '/permission/page',
  //   alwaysShow: true, // will always show the root menu
  //   name: 'Permission',
  //   meta: {
  //     title: '权限管理',
  //     icon: 'lock',
  //     roles: ['admin', 'editor'] // you can set roles in root nav
  //   },
  //   children: [
  //     {
  //       path: 'page',
  //       component: () => import('@/views/permission/page'),
  //       name: 'PagePermission',
  //       meta: {
  //         title: '权限设置',
  //         roles: ['admin'] // or you can only set roles in sub nav
  //       }
  //     },
  //     {
  //       path: 'directive',
  //       component: () => import('@/views/permission/directive'),
  //       name: 'DirectivePermission',
  //       meta: {
  //         title: '权限说明'
  //         // if do not set roles, means: this page does not require permission
  //       }
  //     },
  //     {
  //       path: 'role',
  //       component: () => import('@/views/permission/role'),
  //       name: 'RolePermission',
  //       meta: {
  //         title: '角色权限',
  //         roles: ['admin']
  //       }
  //     }
  //   ]
  // },
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
